<?php
namespace Admin\Controller;
use Admin\Controller\BaseController;

class AdministratorController extends BaseController {

	public function index() {
		$this->assign('administrators', M('Administrator')->select());
		$this->display();
	}

	public function add() {
		$this->display();
	}

	public function addPost() {
		if(!IS_POST) {
			$this->redirect('Administrator/index');
		}
		$administrator = D('Administrator');
		if(!$administrator->create()) {
			$this->error($administrator->getError());
		}
		if(!$administrator->add()) {
			$this->error('数据写入失败！');
		}
		$this->success('添加成功', U('Administrator/index'), 0);
	}

	public function detail() {
		$id = (int)I('id');
		$this->assign('administrator', M('Administrator')->where(array('id' => $id))->find());
		$this->display();
	}

	public function modify() {
		$id = (int)I('id');
		$this->assign('administrator', M('Administrator')->where(array('id' => $id))->find());
		$this->display();
	}

	public function modifyPost() {
		if(!IS_POST) {
			$this->redirect('Administrator/index');
		}
		$administrator = D('Administrator');
		if(!$administrator->create()) {
			$this->error($administrator->getError());
		}
		$result = $administrator->save();
		if(false === $result) {
			$this->error('数据写入失败！');
		}
		$this->success('修改成功！', U('Administrator/index'), 0);
	}

	public function deletePost() {
		$id = (int)I('id');
		if(!IS_POST) {
			$this->redirect('Administrator/index');
		}
		if(M('Administrator')->where(array('id' => $id))->delete()) {
			$this->success('删除成功！', U('Administrator/index'), 0);
		} else {
			$this->error('删除失败！');
		}
	}

}
